﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using VPLink2013.Framework;
using VPLink2013.Framework.Constants;
using VPLink2013.Framework.Extensions;
using System.Collections.Generic;

namespace VPLink2013.Webparts
{
    public class EspaceJediDisplayDetails : WebpartVPLink
    {
        public string strUrl { get; set; }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
        WebDisplayName("XSl d'affichage")]
        public string strXslFilePath { get; set; }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
        WebDisplayName("Url de la liste")]
        public string listUrl { get; set; }

        protected override string GetHtml()
        {
            string strHtml = string.Empty;
            string strXslt = string.Empty;
            string strID = ParamFrm("itemId");
            ContextTrace.PrefixFileName = string.Format("Detail espace jedi id = {0}", strID);
            if (!string.IsNullOrEmpty(strID))
            {
                int id = int.Parse(strID);
                strUrl = SPContext.Current.Web.Url;
                global = new GlobalVPLink2013();
                global.ListeAnnonces = new List<Annonce>();
                try
                {
                    using (SPSite site = new SPSite(strUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            if (!string.IsNullOrEmpty(listUrl))
                            {
                                SPList targetList = web.GetList(listUrl);
                                if (targetList != null)
                                {
                                    SPListItem itemToDisplay = targetList.GetItemById(id);
                                    Annonce announce = new Annonce();
                                    announce.ID = strID;
                                    announce.Titre = itemToDisplay.GetItemListFieldValueAsText("Titre");
                                    announce.TitrePiwik = announce.Titre.Replace("/", "-").Replace("'", "\\'").Replace("\"", "\\'");
                                    announce.Chapeau = itemToDisplay.GetItemListFieldValueAsHtml("Corps");
                                    if (string.IsNullOrEmpty(announce.Chapeau))
                                    {
                                        announce.Chapeau = itemToDisplay.GetItemListFieldValueAsHtml("Description");
                                    }
                                    announce.DatePublication = itemToDisplay.GetItemListFieldValueAsDateTime("Date publication");
                                    if (string.IsNullOrEmpty(announce.DatePublication))
                                    {
                                       announce.DatePublication = itemToDisplay.GetItemListFieldValueAsDateTime("Date");
                                    }
                                    announce.LinkDetail = web.Url;
                                    global.ListeAnnonces.Add(announce);
                                    SPSecurity.RunWithElevatedPrivileges(delegate()
                                    {
                                        strXslt = web.GetFileAsString(strXslFilePath);
                                    });
                                }
                            }
                        }
                    }
                    string strXml = FileSerialization.CreateXmlStringFromObj(global);
                    if (!string.IsNullOrEmpty(strXslt) && !string.IsNullOrEmpty(strXml))
                    {
                        strHtml = GetTransformFromStyleLibrary(strXml, strXslt);
                    }
                }
                catch (Exception ex)
                {
                    ContextTrace.AddTrace(string.Format("Exception : \n{0}", ex.ToString()));
                }
            }
            return strHtml;
        }
    }
}
